package service

import (
	"choujiang/common/models"
	"choujiang/common/db"
	"encoding/json"
	"fmt"
	"time"
)

const REGION_CACHE_KEY = "BoxKey::leyouboxregion_cache"

func GetRegionList() []model.Region {
	keyExist := db.MainRedisClient.Exists(REGION_CACHE_KEY)
	regions := make([]model.Region, 0)
	if keyExist.Val() == 0 {
		err := db.MysqlEngine.In("region_type", []int{1, 2, 3}).Find(&regions)
		if err != nil {
			fmt.Printf("GetRegionList err:%+v\n", err)
		}
		jsonSlice, _ := json.Marshal(regions)
		db.MainRedisClient.Set(REGION_CACHE_KEY, jsonSlice, time.Hour * 3)
	} else {
		regionRedist := db.MainRedisClient.Get(REGION_CACHE_KEY)
		json.Unmarshal([]byte(regionRedist.Val()), &regions)
	}
	return regions
}
